const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    courseLibraryId: '',
    from: '',
    chapterInfo: '',
    chapterList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { id, courseLibraryId, from } = options
    console.log(courseLibraryId)
    this.setData({
      id,
      courseLibraryId,
      from
    })
    console.log(from)
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCourseDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCourseDetail () {
    app.api.post({
      url: 'vx_study_course_detail',
      data: {id: this.data.id}
    }).then(res => {
      this.setData({
        chapterInfo: res,
        chapterList: res.chapterDetailVxs ? res.chapterDetailVxs : []
      })
      console.log(res)
    })
  },
  toLearn (e) {
    console.log('toLearn')
    console.log(e)
    const permissionCodes = app.globalData.permissionCodes || []
    if(!permissionCodes.includes('study_study')){
      wx.showModal({
        content: '无权限学习,请联系管理员',
        showCancel: false
      })
      return
    }
    const { obj } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/learn/learn-pdf/learnPdf?id=${obj.id}&fileType=${obj.fileType}`,
    })
  },
  toTrain () {
    console.log(this.data.chapterInfo)
    wx.navigateTo({
      // url: `/pages/train/appointment/appointment?sceneId=${this.data.chapterInfo.assessmentId}&sceneName=${this.data.chapterInfo.assessmentName}&from=assessment`,
      url: `/pages/train/only-train/onlyTrain?courseId=${this.data.chapterInfo.id}&sceneId=${this.data.chapterInfo.assessmentId}&sceneName=${this.data.chapterInfo.assessmentName}&from=learning`,
    })
  },
  prev () {
    if (this.data.chapterInfo.preCourseId) {
      this.setData({
        id: this.data.chapterInfo.preCourseId
      })
      this.getCourseDetail()
      console.log(this.data.chapterInfo.preCourseId)
    }
  },
  next () {
    if (this.data.chapterInfo.nextCourseId) {
      this.setData({
        id: this.data.chapterInfo.nextCourseId
      })
      this.getCourseDetail()
      console.log(this.data.chapterInfo.preCourseId)
    }
  },
  goToCourse () {
    wx.navigateTo({
      url: `/pages/learn/course/course`,
    })
  }
})